package com.uphone.kingmall.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.china.zhuoyue.R;
import com.uphone.kingmall.bean.GoodsDetailBean;
import com.uphone.kingmall.listener.OnItemClickListener2;
import com.uphone.kingmall.view.MultipleTextViewGroup;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by sumeng on 2019/2/25.
 * 选择规格
 */
public class ShopsCartPwAdapter extends RecyclerView.Adapter<ShopsCartPwAdapter.ViewHolder> {

    private Context context;
    private List<GoodsDetailBean.GoodsBean.PropsBean> list;

    public ShopsCartPwAdapter(Context context, List<GoodsDetailBean.GoodsBean.PropsBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.kingmall_item_goodspw1, parent, false));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

//        holder.flex_layout.setHorizontalSpace(15);
//        holder.flex_layout.setVerticalSpace(10);
        holder.flex_layout.removeAllViews();
        GoodsDetailBean.GoodsBean.PropsBean propsBean = list.get(position);
        TextView[] tvs = new TextView[propsBean.getProp().size()];
        for (int i = 0; i < propsBean.getProp().size(); i++) {
            GoodsDetailBean.GoodsBean.PropsBean.PropBean propBean = propsBean.getProp().get(i);
            final TextView textView = new TextView(context);
            textView.setText(propBean.getGoodsPropVal());
            textView.setTextSize(14);
            if (propBean.isSelect() == 1) {
                textView.setTextColor(context.getResources().getColor(R.color.colorTextSelect));
                textView.setBackgroundResource(R.drawable.shape_bg_ffeff1_red_yuan);
            } else {
                textView.setTextColor(context.getResources().getColor(R.color.colorText));
                textView.setBackgroundResource(R.drawable.shape_bg_eeeeee_yuan);
            }
            textView.setPadding(20, 10, 20, 10);
//            holder.flex_layout.addView(textView);
            final int finalI = i;
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    itemClickListener2.onItemClick(v, position, finalI);
                    for (int j = 0; j < holder.flex_layout.getChildCount(); j++) {
                        View childAt = holder.flex_layout.getChildAt(j);
                        if (childAt instanceof TextView) {
                            ((TextView) childAt).setTextColor(context.getResources().getColor(R.color.colorText));
                            childAt.setBackgroundResource(R.drawable.shape_bg_eeeeee_yuan);
                        }
                        if (v instanceof TextView) {
                            ((TextView) v).setTextColor(context.getResources().getColor(R.color.colorTextSelect));
                            v.setBackgroundResource(R.drawable.shape_bg_ffeff1_red_yuan);
                        }
                    }
                }
            });
            tvs[i] = textView;
        }
        holder.flex_layout.setTextViews(tvs);
        for (int i = 0; i < 0; i++) {
            TextView textView = new TextView(context);
            textView.setText("测试测试测试测试测试测试测试");
            textView.setTextSize(14);
            textView.setTextColor(context.getResources().getColor(R.color.colorText));
            textView.setBackgroundResource(R.drawable.shape_bg_eeeeee_yuan);
            textView.setPadding(20, 10, 20, 10);
            holder.flex_layout.addView(textView);
            final int finalI = i;
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    itemClickListener2.onItemClick(v, position, finalI);
                    for (int j = 0; j < holder.flex_layout.getChildCount(); j++) {
                        View childAt = holder.flex_layout.getChildAt(j);
                        if (childAt instanceof TextView) {
                            ((TextView) childAt).setTextColor(context.getResources().getColor(R.color.colorText));
                            childAt.setBackgroundResource(R.drawable.shape_bg_eeeeee_yuan);
                        }
                        if (v instanceof TextView) {
                            ((TextView) v).setTextColor(context.getResources().getColor(R.color.colorTextSelect));
                            v.setBackgroundResource(R.drawable.shape_bg_ffeff1_red_yuan);
                        }
                    }
                }
            });
        }
        holder.flex_layout.measure(0, 0);

        holder.tvTypeName.setText(list.get(position).getPropName());
//        ShopsCartPwAdapter2 shopsCartPwAdapter2 = new ShopsCartPwAdapter2(context,list.get(position).getProp());
//        holder.rvType.setLayoutManager(new GridLayoutManager(context, 2));
//        holder.rvType.setAdapter(shopsCartPwAdapter2);
//        shopsCartPwAdapter2.setOnItemClickListener(new OnItemClickListener() {
//            @Override
//            public void onItemClick(View v, int position2) {
//                itemClickListener2.onItemClick(v,position,position2);
//            }
//        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_type_name)
        TextView tvTypeName;
        @BindView(R.id.rv_type)
        RecyclerView rvType;
        @BindView(R.id.flex_box_layout)
        MultipleTextViewGroup flex_layout;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    OnItemClickListener2 itemClickListener2;

    public void setOnItemClickListener2(OnItemClickListener2 itemClickListener) {
        this.itemClickListener2 = itemClickListener;
    }


}
